package com.example.royalchef.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Process;
import android.view.View;

/**
 * Created by Administrator on 2017/3/9.
 *
 */

public class UIUtils {

    public static Context getContext() {
        return MyApplication.getContext();
    }

    public static Handler getHandler() {
        return MyApplication.getHandler();
    }

    public static int getMainTheadId() {
        return MyApplication.getMyTid();
    }

    /////////////////获取资源文件/////////////////

    //获得string
    public static String getString(int id) {
        return getContext().getResources().getString(id);
    }

    //获得string数组
    public static String[] getStringArray(int id) {
        return getContext().getResources().getStringArray(id);
    }

    //获得图片资源
    public static Drawable getDrawable(int id) {
        return getContext().getResources().getDrawable(id);
    }

    //获得大小资源
    public static float getDimen(int id) {
        return getContext().getResources().getDimension(id);
    }

    //获得颜色资源
    public static int getColor(int id) {
        return getContext().getResources().getColor(id);
    }

    /////////////////////加载布局////////////////////
    public static View inflate(int id) {
        View view = View.inflate(getContext(), id, null);
        return view;
    }

    ////////////////////dp和px相互转化////////////////

    public static int dip2px(int dp) {
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * density);
    }

    public static int px2dip(int px) {
        float density = getContext().getResources().getDisplayMetrics().density;
        return (int) (px / density);
    }

    ///////////////////判断是否在主线程,////////////////

    public static boolean isRunMainThead() {
        int tid = Process.myTid();
        if (tid == getMainTheadId()) {
            return true;
        } else {
            return false;
        }
    }

    /////////////////////让她在主线程中运行///////////////
    public static void runMainThread(Runnable runnable) {
        if (isRunMainThead()) {
            runnable.run();
        } else {
            getHandler().post(runnable);
        }
    }

    public static ColorStateList getColorStateList(int id) {
        return getContext().getResources().getColorStateList(id);
    }
}
